import { ICommand } from "./ICommand";

/**
 * 渲染命令管理器
 *
 * 主要目的是加速渲染命令的组装
 */
export class CommandManager {
    private static ins = new CommandManager();
    private constructor() {
        1;
    }
    public static getIns(): CommandManager {
        return this.ins;
    }

    private cmdCacheMap: Map<string, ICommand> = new Map();
    private registerMap: Map<string, ICommand> = new Map();
    getCmd(cmdName: string): ICommand {
        if (!this.cmdCacheMap.has(cmdName)) {
            const registered = this.registerMap.get(cmdName);
            if (registered) {
                this.cmdCacheMap.set(cmdName, registered);
            }
        }
        const cmd = this.cmdCacheMap.get(cmdName)!;
        if (cmd && cmd.reset) {
            cmd.reset();
        }
        return cmd;
    }
    registerCmd(cmdName: string, cmd: ICommand) {
        this.registerMap.set(cmdName, cmd);
    }
}